#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/errno.h>



int main(int argc,const char*  argv[]){

    key_t key = ftok("./app",'a');
    if(key < 0){
        perror("ftok err");
        return -1;
    }
    printf("%#x\n",key);

    int shmid = shmget(key,128,IPC_CREAT | IPC_EXCL | 0666);
    if(shmid <= 0){
        if(errno == EEXIST){
            shmid = shmget(key,128,0666);
        }else{
            perror("shmget err");
            return -1;
        }
    }

    printf("shmid:%d\n",shmid);

    char * p=(void*)shmat(shmid,NULL,0);
    if(p == (char*)-1){
        perror("shmat err");
        return -1;
    }

    shmdt(p);

    // shmctl(shmid,IPC_RMID,NULL);
    return 0;
}